var leid = localStorage.getItem('listenLessonID');
var db = window.openDatabase("JapaneseBasic", "1.0", "Japanese Database",500000);;
// connect to database
function connectDB() {
	db.transaction(getlisteningByLesson, errorCB);
}
// function will be called when an error occurred
function errorCB(err) {
	console.log("listening.js Error processing SQL: " + err.code);
}
//call when swich lesson
function swichLesson(){
	leid = localStorage.getItem('listenLessonID');
	connectDB();
}

// get all lesson from db
function getlisteningByLesson(tx) {
	var sql = "SELECT li_id,li_title, li_script_jp, li_track, li_script_en, li_script_vi, li_lesson_id, li_dataversion_id "
			+ "FROM listening "
			+ "WHERE li_lesson_id = ?";
	tx.executeSql(sql, [leid], showlisteningByLesson);
	console.log("listening.js SQL: "+sql + leid)
}

// show lesson list in html code
function showlisteningByLesson(tx, results) {
	var len = results.rows.length;
	$('#script-content-jp').empty();
	$('#script-content-en').empty();
	$('#script-content-vi').empty();
	if(len != 0){
		var data = results.rows.item(0);
		console.log("listening.js sql result script: "+data.li_script_jp +" vi:"+  data.li_script_en +" vi:"+ data.li_script_vi);
		$('#script-content-jp').append('<p>'+data.li_script_jp+'<p>');
		$('#script-content-en').append('<p>'+data.li_script_en+'<p>');
		$('#script-content-vi').append('<p>'+data.li_script_vi+'<p>');
	}	else{
		$('#script-content-jp').append('<p>Hiện chưa có bài nghe<p>');
		$('#script-content-en').append('<p>Hiện chưa có bài nghe<p>');
		$('#script-content-vi').append('<p>Hiện chưa có bài nghe<p>');
	}
}


function isFavourite(id){
	db.transaction(function(tx){
		var sql = "SELECT li_isFavourite FROM listening WHERE li_id = '"+id + "'";
		console.log(sql);
		tx.executeSql(sql, [], function(tx,results){
			var len = results.rows.length;
			if(len != 0){
				var data = results.rows.item(0).li_isFavourite;
				return data;
			}
		},errorCB);
	}, errorCB);
}
function setFavourite(id){
	alert("sddf");
	/*db.transaction(function(tx){
			var sql = "UPDATE listening SET li_isFavourite = '1' WHERE li_id = '"+id + "'";
			console.log(sql);
			tx.executeSql(sql)
		}, errorCB);*/
}

function updateFavourete(id){
	//lay ve gia tri hien tai cua word co id = id 
	var sql = "SELECT * "
		    + "FROM listening WHERE v_id = ?";
	
    tx.executeSql(sql, [id], getFavourite,errorCB);
    
    console.log(sql);
    
	//update record and change icon
	//tx.executeSql("UPDATE listening SET data ='" +submitval+"' WHERE id = '"+8+"' ;", [],   updateCB, errorCB);
}



function setFavourite(id,checked){
	
}

function getFavourite(tx, results){
	 console.log("Insert ID = " + results.insertId);
	    // this will be 0 since it is a select statement
	 console.log("Rows Affected = " + results.rowAffected);
	    // the number of rows returned by the select statement
	 console.log("Insert ID = " + results.rows.length);
}

